package cn.z2huo.demo.redis.hutool.masterslave;

import cn.hutool.db.nosql.redis.RedisDS;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;

/**
 * <p>
 *
 * @author z2huo
 */
@Slf4j
public class MasterSlaveTest {

    public String masterWrite() {
        try (RedisDS redisDS = RedisDS.create("master-slave-master")) {

            String key = RandomStringUtils.randomAlphanumeric(8);
            log.info("key: {}", key);
            redisDS.setStr(key, RandomStringUtils.randomAlphabetic(16));
            return key;
        }
    }

    public void masterWriteAndRead() {

        String key = this.masterWrite();

        try (RedisDS redisDS = RedisDS.create("master-slave-master")) {
            String str = redisDS.getStr(key);
            log.info("redis select result is: {}", str);
        }
    }

    public void slaveWrite() {
        try (RedisDS redisDS = RedisDS.create("master-slave-slave-01")) {

            String key = RandomStringUtils.randomAlphanumeric(8);
            log.info("key: {}", key);
            redisDS.setStr(key, RandomStringUtils.randomAlphabetic(16));

            String str = redisDS.getStr(key);
            log.info("redis select result is: {}", str);
        }
    }

    public void slaveRead(String key) {
        try (RedisDS redisDS = RedisDS.create("master-slave-slave-02")) {
            String str = redisDS.getStr(key);
            log.info("redis select result is: {}", str);
        }
    }

}
